package co.yixiang.yshop.module.store.job;

import co.yixiang.yshop.framework.common.enums.ShopCommonEnum;
import co.yixiang.yshop.framework.quartz.core.handler.JobHandler;
import co.yixiang.yshop.module.store.dal.dataobject.storerevenue.StoreRevenueDO;
import co.yixiang.yshop.module.store.dal.mysql.storerevenue.StoreRevenueMapper;
import co.yixiang.yshop.module.store.dal.mysql.storeshop.StoreShopMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 店铺收入结算定时任务每天晚上12点执行
 */
@Component
public class RevenueJob implements JobHandler {

    @Resource
    private StoreRevenueMapper storeRevenueMapper;
    @Resource
    private StoreShopMapper storeShopMapper;

    @Override
    public String execute(String param) throws Exception {
        System.out.println("结算定时任务start================");
        //每天只执行100条数据，按照时间倒叙
        List<StoreRevenueDO> storeRevenueDOList = storeRevenueMapper.selectList(new LambdaQueryWrapper<StoreRevenueDO>()
                .eq(StoreRevenueDO::getIsFinish, ShopCommonEnum.IS_FINISH_0.getValue())
                .orderByDesc(StoreRevenueDO::getCreateTime).last("limit 100"));

        for (StoreRevenueDO storeRevenueDO : storeRevenueDOList) {
            //更新金额
            storeShopMapper.incMoney(storeRevenueDO.getShopId(),storeRevenueDO.getAmount());
            //状态更新
            storeRevenueDO.setIsFinish(ShopCommonEnum.IS_FINISH_1.getValue());
            storeRevenueMapper.updateById(storeRevenueDO);
        }

        return "店铺结算更新数量：" + storeRevenueDOList.size();
    }

}
